package com.bjwl.web.contrlller.admin;

import com.bjwl.pojo.Admin;
import com.bjwl.pojo.BranchStore;
import com.bjwl.pojo.Staff;
import com.bjwl.pojo.dto.BrDto;
import com.bjwl.pojo.vo.QueryVoFinsBranchStore;
import com.bjwl.service.*;
import com.bjwl.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.annotation.RequestScope;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: dzx
 * @Date:Create：in 2020/4/10 23:16
 * @Description:    总公司视图层
 * @Modified By：
 * @Version: 1.0
 */
@Controller
@RequestMapping("/admin/main")
public class AdminContrlller {


    @Autowired
    private AdminService adminService;
    @Autowired
    private BranchStoreService branchStoreService;
    @Autowired
    private BusinessService businessService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private StaffService staffService;

    /**
     * 登录
     * @param
     * @return
     */
    @RequestMapping("/adminLogin")
    public @ResponseBody boolean adminLogin(Admin admin, HttpSession session){
        Admin admin1=adminService.findAdminByUserNameAndPassWord(admin);
        if(admin1!=null && admin1.getState().equals(1)){
            session.setAttribute("admin",admin1);
            Integer BranchStoretSize = branchStoreService.findAllBranchStoretTotalRecord();
            Integer BusinessSize =  businessService.findAllBusinessTotalRecord();
            Integer OrdersSize =  ordersService.findAllOrdersTotalRecord();
            Integer StaffSize =  staffService.findAllStaffTotalRecord();
            Integer AdminSize =  adminService.findAllAdminTotalRecord();
            List<BrDto> brDtos = branchStoreService.findAllBranchStore();
            session.setAttribute("brDtos",brDtos);
            session.setAttribute("BranchStoretSize",BranchStoretSize);
            session.setAttribute("BusinessSize",BusinessSize);
            session.setAttribute("OrdersSize",OrdersSize);
            session.setAttribute("StaffSize",StaffSize);
            session.setAttribute("AdminSize",AdminSize);
            return true;
        }
        return false;
    }

    /**
     * 主页当前用户修改
     */

    @RequestMapping("/updateAdmin")
    public @ResponseBody boolean updateAdmin(@RequestBody Admin admin, HttpSession session){
        try {
            adminService.ByIdUpdateuser(admin);
            Admin admin1 = adminService.findAdminById(admin);
            session.removeAttribute("admin");
            session.setAttribute("admin",admin1);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    /**
     * 修改前回显
     */
    @RequestMapping("/updateAdmins")
    public String updateAdmins(Admin admin,Model model){
            Admin admins = adminService.findAdminById(admin);
            model.addAttribute("admins",admins);
        return "main_oa/admin-edit";
    }

    /**
     * 修改
     */
    @RequestMapping("/updateAdmine")
    public @ResponseBody Boolean updateAdmins(@RequestBody Admin admin){
        adminService.ByIdUpdateuser(admin);

        return true;
    }

    /**
     * 退出登录
     */
    @RequestMapping("/adminCancellation")
    public String adminCancellation(HttpSession session){
        session.removeAttribute("admin");
        return "main_oa/login";
    }

    /**
     * 查询所有管理员，分页
     */
    @RequestMapping("/findAllAdmin")
    public String findAllAdmin(Integer pageNum,String username,Model model){
        if(pageNum == null){
            pageNum = 1;
        }
        PageBean<Admin> pageBean = adminService.findAllAdmin(pageNum,username);
        model.addAttribute("pageBean",pageBean);
        return "main_oa/admin-list";
    }

    /**
     * 添加管理员
     */
    @RequestMapping("/addAdmin")
    public @ResponseBody Boolean addAdmin(@RequestBody Admin admin){
        List<Admin> admins = adminService.findAdminByUserName(admin.getUserName());
        if(admins.size() == 0){
            adminService.addAdmin(admin);
            return true;
        }

        return false;
    }
    /**
     * 删除管理员
     */
    @RequestMapping("/deleteAdminById")
    public @ResponseBody Boolean deleteAdminById(Integer id){
        adminService.deleteAdmin(id);
        return true;
    }

    /**
     * 停用启用管理员
     */
    @RequestMapping("/adminState")
    public @ResponseBody Boolean adminState(Admin admin){
        adminService.adminState(admin);
        return true;
    }

}
